import AlertComponent from './Alert.vue'

const Alert = {}

Alert.install = (Vue) => {
  Vue.component('alert', AlertComponent)
  // 根据AlertComponent创建一个vue子类
  const AlertConstructor = Vue.extend(AlertComponent)
  // 实例化子类
  const instance = new AlertConstructor()
  // 将子类挂载到div
  instance.$mount(document.createElement('div'))
  document.body.appendChild(instance.$el)
  // 1. 添加全局方法或 property
  Vue.prototype.$alert = function (msg) {
    instance.type = 'alert'
    instance.isShow = true
    instance.msg = msg
    const result = this.$watch(() => {
      return instance.self_isShow
    }, (val) => {
      if (!val) {
        instance.isShow = false
        result()
      }
    })
  }
  Vue.prototype.$confirm = function (msg, confirm, cancel) {
    instance.type = 'confirm'
    instance.isShow = true
    instance.msg = msg
    if (cancel) {
      instance.cancel = cancel
    }
    if (confirm) {
      instance.confrim = confirm
    }
    const result = this.$watch(() => {
      return instance.self_isShow
    }, (val) => {
      if (!val) {
        instance.isShow = false
        result()
      }
    })
  }
}

export default Alert
